/*
 * Copyright (c) 2015 www.jd.com All rights reserved.
 * 本软件源代码版权归京东智能集团所有,未经许可不得任意复制与传播.
 */
package com.test;

import cn.hutool.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * <b>描述：</b> <br/>
 *
 * @author <b>作者：</b> wutao91@jd.com<br/>
 * @date <b>时间：</b> 2021/7/16 14:55<br/>
 * <b>Copyright (c)</b> 2015-2021京东智能-版权所有<br/>
 */
public class Client {
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 8007;
    private static final String LOCK = new String("lock");
    
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(HOST, PORT));
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        int len = inputStream.available();
                        if (len == 0) {
                            continue;
                        }
                        byte[] input = new byte[len];
                        inputStream.read(input);
                        String data = new String(input);
                        System.out.println(data);
                        if ("注册成功".equals(data)) {
                            synchronized (LOCK) {
                                LOCK.notifyAll();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject data = new JSONObject();
                    data.put("type", 1);
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("注册名字：");
                    String name = scanner.nextLine();
                    data.put("name", name);
                    outputStream.write(data.toString().getBytes(StandardCharsets.UTF_8));
                    outputStream.flush();
                    synchronized (LOCK) {
                        LOCK.wait();
                    }
                    data.put("type", 2);
                    data.put("from", name);
                    while (true) {
                        String input = scanner.nextLine();
                        String[] inputs = input.split(" ");
                        data.put("to", inputs[0]);
                        data.put("msg", inputs[1]);
                        outputStream.write(data.toString().getBytes(StandardCharsets.UTF_8));
                        outputStream.flush();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
